package com.csjung.games.gamenori.client.ui;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.PartialSupport;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.Composite;

import com.csjung.games.gamenori.shared.GamenoriRequestFactory;
import com.csjung.games.gamenori.shared.UserProxy;

import java.util.List;

@PartialSupport
public class DestopUserList extends Composite {

  private class UserListCell extends AbstractCell<String> {

    public UserListCell() {
      super(BrowserEvents.MOUSEOVER, BrowserEvents.MOUSEOUT);
    }

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
      if (value != null) {
        SafeHtml safeValue = SafeHtmlUtils.fromString(value);
        sb.append(safeValue);
      }
    }
  }
  
  /**
   * Displays the list of tasks.
   */
  DataGrid<UserProxy> userList;
  
  UserInfoPopup userInfoPopup;

  public DestopUserList(final GamenoriRequestFactory requestFactory) {
    // Create the CellTable.
    userList = new DataGrid<UserProxy>();
    userList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
    userList.setWidth("100%");
    
    UserListCell cell = new UserListCell();
    Column<UserProxy, String> clickColumn = new Column<UserProxy, String>(cell) {
      @Override
      public String getValue(UserProxy object) {
        return (object == null) ? null : object.getNickname();
      }
      
      @Override
      public void onBrowserEvent(Context context, Element elem, UserProxy object,
          NativeEvent event) {
        super.onBrowserEvent(context, elem, object, event);
        String eventType = event.getType();
        if (BrowserEvents.MOUSEOVER.equals(eventType)) {
          if (userInfoPopup != null) {
            if (!object.getUserId().equals(userInfoPopup.getUserProxy().getUserId()))
              userInfoPopup.hide();
              userInfoPopup = new UserInfoPopup(object, requestFactory);
              int left = elem.getAbsoluteLeft() + 150;
              int top = elem.getAbsoluteTop();
              userInfoPopup.setPopupPosition(left, top);
              userInfoPopup.show();
            }
          } else {
            userInfoPopup = new UserInfoPopup(object, requestFactory);
            int left = elem.getAbsoluteLeft() + 150;
            int top = elem.getAbsoluteTop();
            userInfoPopup.setPopupPosition(left, top);
            userInfoPopup.show();
          }
        } 
    };
    
    userList.addColumn(clickColumn, "Users");
    
    // Initialize the widget.
    initWidget(userList);
  }


  public void clearList() {
    userList.setVisibleRangeAndClearData(userList.getVisibleRange(), true);
  }

  public void setUsers(List<UserProxy> users) {
    userList.setRowData(users);
  }
}
